Preview - Intro To Netlogo Programming For Educators

Patches


This section will introduce you to patches, the second main element of NetLogo.


Questions

Please answer the questions below.

Changing Patch Color: pcolor

Patches form the background of any NetLogo model. Right now they are all black, so the "view" just looks like a big black box, but it is actually made of many patches. You can ask patches to do things just like you can ask turtles to.

  • Patches have a color property that can be changed, similar to turtles, but it is called pcolor. Ask the patches to change their color like this ask patches [set pcolor color].  Individual patches are identified by x and y coordinates. For instance you could ask patch 0 0 [set pcolor color].

 

  • Another way to represent colors in NetLogo is as a number between 0 and 140. For example, you could ask a patch to [set pcolor 20].

 

  • Each patch has a property called pxcor and pycor that refers to its x and y coordinates. Try writing a procedure to ask patches to set their pcolor based on their pxcor and/or pycor. Copy your procedure below.

 


Asking Neighbors for Things

Patches can ask other patches to do things (Turtles can too). This is one way to have patches interact with on another.

  • Ask a specific patch to ask its neighbors to set their pcolor like this ask patch 5 3 [ask neighbors [set pcolor color]]. (where color is a specific color). 
  • Whenever you ask a patch to do something, any properties inside the brackets (like pcolor) belong to the patch being asked. So, in the code in the previous bullet point, each neighbor of patch 5 3 was asked to set its own pcolor. But what if patch 5 3 wants to ask its neighbors to change their pcolor to match its own? For this, it can ask the neighbors to set their pcolor to [pcolor] of myself. This is like saying "set (your) pcolor to the pcolor of me." 
  • Try asking a specific patch to ask its neighbors to set their pcolor to its pcolor. Copy and paste the code you used to accomplish this.

 


Procedures with Patches

Now you will write some procedures for patches.

  • Write a setup procedure, and create a button for it, that sets the world up with patches of at least two different colors. You could do this by setting pcolor based on pxcor/pycor,  by using a random number, or any other way you think of. 

Copy and paste your setup procedure below. 


Go procedure

  • Now write a go procedure that asks patches to ask their neighbors to change their color. You can ask all 8 neighbors with neighbors, or just the east, west, north and south neighbors with neighbors4. You can also ask just one of these neighbors by asking one-of neighbors to do something.
  • Make a button for the go procedure. Have it run forever, and also disable it until ticks start.

Copy the code of your go procedure below.

What behavior do you see? Why does this happen? Describe below.

 


If you had any issues on this page, describe them below. If not, type something like "no problems" and continue on.


Notes

These notes will appear on every page in this lesson so feel free to put anything here you'd like to keep track of.